/* 6. 查询书籍 */

"use strict";

const CHECK = require("../myUtil/formatCheck");
const db = require("../../coSqlite3");
const HTM = require("../myUtil/lib").html;

exports.queryBook = function* (req, res) {
    console.log("----------------------6. 查询书籍\n");
    let bID = req.body.bID;
    let bName = req.body.bName;
    let bPub = req.body.bPub;
    let bDate0 = req.body.bDate0;
    let bDate1 = req.body.bDate1;
    let bAuthor = req.body.bAuthor;
    let bMem = req.body.bMem;


    let htm = HTM.tableBegin + HTM.tableEnd + "提交的参数有误：";
    if (bDate0 != '') {
      if (!CHECK.dataFormatCheck(bDate0)) {
        return HTM.head + htm + "出版日期格式不正确" + HTM.end;
      }
    }
    if (bDate1 != '') {
        if (!CHECK.dataFormatCheck(bDate1)) {
            return HTM.head + htm + "出版日期格式不正确" + HTM.end;
        }
    }

    let array = [bID, bName, bPub, bDate0, bDate1, bAuthor, bMem];
    let args = [];
    let trueORFalse = [];
    for (let item of array) {
        if (item != '') {
            args.push(item);
            trueORFalse.push(true);
            continue;
        }
        trueORFalse.push(false);
    }
    // console.log(args);
    // console.log(trueORFalse);
    // console.log(trueORFalse[0]);
    // console.log(trueORFalse[1]);
    // console.log(trueORFalse[2]);
    // console.log(trueORFalse[3]);
    // console.log(trueORFalse[4]);
    // console.log(trueORFalse[5]);
    // console.log(trueORFalse[6]);

    let rows;
    try {
        rows = yield db.execSQL(
            "SELECT * FROM book WHERE 1=1 " +
            (trueORFalse[0] ? "AND bID LIKE '%'||?||'%' " : "") +
            (trueORFalse[1] ? "AND bName LIKE '%'||?||'%' " : "") +
            (trueORFalse[2] ? "AND bPub LIKE '%'||?||'%' " : "") +
            (trueORFalse[3] ? "AND bDate>=? " : "") +
            (trueORFalse[4] ? "AND bDate<=? " : "") +
            (trueORFalse[5] ? "AND bAuthor LIKE '%'||?||'%' " : "") +
            (trueORFalse[6] ? "AND bMem LIKE '%'||?||'%' " : ""),
            args);

    } catch (e) {
        htm = HTM.tableBegin + HTM.tableEnd + "数据库执行失败: " + e;
        return HTM.head + htm + HTM.end;
    }

    if (rows.length == 0) {
        // console.log("empty data")
        htm =  HTM.tableBegin + HTM.tableEnd;
        return HTM.head + htm + HTM.end;
    }

    htm = ''
    // htm = '<tr><td>1.书号</td><td>2.书名</td><td>3.总数量</td><td>4.在库数量</td><td>5.出版社</td><td>6.出版日期</td><td>7.作者</td><td>8.内容摘要</td></tr>';
    for (let row of rows) {
        // console.log(row.bid);
        htm +=
          "<tr><td>" +
          row.bid +
          "</td><td>" +
          row.bname +
          "</td><td>" +
          row.bcnt +
          "</td><td>" +
          row.btruecnt +
          "</td><td>" +
          row.bpub +
          "</td><td>" +
          row.bdate +
          "</td><td>" +
          row.bauthor +
          "</td><td>" +
          row.bmem + 
          "</td></tr>\n";
    }

    return (
      HTM.head + HTM.tableBegin + htm + HTM.tableEnd + HTM.end
    );
}